package com.gimi.cloud.jimi.commonService.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.gimi.cloud.jimi.common.enums.common.RedisKeyEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.commonService.service.CoreSecurityInstructionsService;
import com.gimi.cloud.jimi.dao.dao.product.SecurityInstructionsDao;
import com.gimi.cloud.jimi.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.jimi.dao.redis.IRedisService;
import com.gimi.cloud.jimi.dto.product.SecurityInstructionsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @auther loongs
 * @date 2018/1/29  15:09
 */
@Service
public class CoreSecurityInstructionsServiceImpl implements CoreSecurityInstructionsService {
    private SecurityInstructionsDao securityInstructionsDao;
    private IRedisService iRedisService;

    @Autowired
    public CoreSecurityInstructionsServiceImpl(SecurityInstructionsDao securityInstructionsDao, IRedisService iRedisService) {
        this.securityInstructionsDao = securityInstructionsDao;
        this.iRedisService = iRedisService;
    }

    @Override
    public List<SecurityInstructionsDTO> getAllBySort() {
        JSONArray jsonArrayConfigByKey = iRedisService.getJSONArrayConfigByKey(RedisKeyEnum.JIMI_APP_PRE.getKey() + "securityInstructions");
        if (jsonArrayConfigByKey == null || jsonArrayConfigByKey.size() == 0) {
            return getSecurityInstructionsFromSql();
        }
        //jsonArry转list
        List<SecurityInstructionsDTO> list = JsonUtil.getListByArray(SecurityInstructionsDTO.class, jsonArrayConfigByKey.toJSONString());
        //将路径改为全路径
        setAllOssPath(list);
        return list;
    }

    /**
     * 从数据库拿，拿了放redis,过期时间是一天
     * @return
     */
    private List<SecurityInstructionsDTO> getSecurityInstructionsFromSql() {
        String sql = " SELECT id,type,title,coment,img_url,sort FROM ec_security_instructions  where 1=1 order by sort asc";
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        queryParam.setParamObject(new SecurityInstructionsDTO());
        List<SecurityInstructionsDTO> list = securityInstructionsDao.selectListDTOBySql(queryParam);
        //list转jsonArry
        JSONArray jsonArray = JsonUtil.getJSONArrayByList(list);
        //缓存到redis
        iRedisService.set(RedisKeyEnum.JIMI_APP_PRE.getKey() + "securityInstructions", jsonArray.toJSONString(), 24L, TimeUnit.HOURS);
        //改成长路径
        if (list != null && list.size() > 0) {
            setAllOssPath(list);
        }
        return list;
    }

    /**
     * 将路径改为全路径
     *
     * @param list
     */
    private void setAllOssPath(List<SecurityInstructionsDTO> list) {
        for (SecurityInstructionsDTO securityInstructionsDTO : list) {
            if (!StringUtil.isBlank(securityInstructionsDTO.getImgUrl())) {
                securityInstructionsDTO.setImgUrl(AliOSSUtil.getFileUrl(securityInstructionsDTO.getImgUrl(), AliOSSUtil.getNoEffictDateByDay(1)));
            }
        }
    }
}
